package com.example.compete.controller;

import com.alibaba.fastjson.JSON;
import com.example.compete.DTO.CompanyAndExecutiveDTO;
import com.example.compete.DTO.CompanyDTO;
import com.example.compete.DTO.DocumentTypeDTO;
import com.example.compete.DTO.ExecutiveDTO;
import com.example.compete.service.CompanyService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/company")

public class CompanyController {

    @Autowired
    CompanyService companyService;

    //查找企业全部信息
    @CrossOrigin
    @GetMapping("goShowAllCompany")
    public List<CompanyDTO> companyDTOList() {
        return companyService.findAll();
    }

    //查看企业类型数据
    @CrossOrigin
    @GetMapping("goShowAllDocumentType")
    public List<DocumentTypeDTO> documentTypeDTOList() {
    	return companyService.findAllDocument();
    }
    
    //查看单条企业信息(id)
    @CrossOrigin
    @RequestMapping("goShowCompany")
    public CompanyDTO companyDTOS(@RequestParam("id") String companyId) {
        return companyService.findByCompanyId(companyId);
    }

    //修改企业信息(id)
    @CrossOrigin
    @RequestMapping("editCompany")
    public void editCompany(@RequestBody CompanyDTO companyDTO) {
        companyService.updateByCompanyId(companyDTO);
    }


    //增加企业信息
    @CrossOrigin
    @RequestMapping("doAddCompany")
    public String addCompany(@RequestBody CompanyDTO companyDTO) {
        return JSON.toJSONString(companyService.add(companyDTO));
    }
    
    //关联删除企业信息和高管信息
    @CrossOrigin
    @GetMapping("deleteCompanyAndExecutive")
    public void deleteCompanyAndExecutive(@RequestParam("id") String companyId) {
        companyService.deleteCompanyAndExecutive(companyId);
    }
    
    //关联查询企业信息和高管信息
    @CrossOrigin
    @GetMapping("selectCompanyAndExecutive")
    public CompanyAndExecutiveDTO selectAllMessageByCompanyId(@RequestParam("id") String companyId){
        return companyService.selectCompanyAndExecutiveByCompanyId(companyId);
    }
    
    //检查输入信息数据库中是否存在
    @CrossOrigin
    @RequestMapping("checkCompany")
    public int checkCompany(@RequestBody CompanyDTO companyDTO) {
    	if(companyService.checkCompany(companyDTO).size()==0) {
    		return 0;//可以加入数据库
    	}else {
    		return 1;
    	}
    }

}
